home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / Apple Color OneScanner SDK / Scan Image 1.0 / Source / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-24  |  8.7 KB  |  415 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        utils.c
  4. #
  5. #        This segment handles file parsing, and basic utility functions.
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    Apple Developer Technical Support
  9. #                    marink@apple.com
  10. #
  11. #        Modification History: 
  12. #
  13. #            4/3/96        MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Events.h>
  29. #include <Finder.h>
  30. #include <ToolUtils.h>
  31. #include <Gestalt.h>
  32. #include <OSUtils.h>
  33. #include <StandardFile.h>
  34.  
  35. #include "App.h"
  36. #include "Proto.h"
  37.  
  38. //----------------------------------------------------------------------
  39. //    Globals
  40. //----------------------------------------------------------------------
  41.  
  42. OSType        gFileType;
  43.  
  44.  
  45. //----------------------------------------------------------------------
  46. //
  47. //    DoOpenNew - query user for new file. Open it and 
  48. //                display in a new window.
  49. //
  50. //----------------------------------------------------------------------
  51.  
  52. void DoOpenNew(void)
  53. {
  54.     OSErr                    err = noErr;
  55.     StandardFileReply        reply;
  56.     SFTypeList                 typeList;
  57.     DlgHookYDUPP            theDialogHookUPP;
  58.     FileFilterYDUPP            theDialogFileFilterUPP;
  59.     WindowRef                window;
  60.     GWorldPtr                theWorld = nil;
  61.     Rect                    bounds;
  62.     Point                    thePt = {-1, -1};
  63.     short                    numTypes;
  64.     DocHnd                    doc;
  65.  
  66.     gFileType = kAllType;
  67.  
  68.     theDialogHookUPP = NewDlgHookYDProc(CustomGetFileHook);
  69.     theDialogFileFilterUPP = NewFileFilterYDProc(CustomFileDlogFilter);
  70.     
  71.     CustomGetFile(theDialogFileFilterUPP, -1,
  72.                   typeList, &reply, 132,
  73.                   thePt, theDialogHookUPP,
  74.                   nil,nil,nil,nil);
  75.                    
  76.     DisposeRoutineDescriptor((UniversalProcPtr)theDialogHookUPP);
  77.     DisposeRoutineDescriptor((UniversalProcPtr)theDialogFileFilterUPP);
  78.  
  79.     if ( reply.sfGood )
  80.     {
  81.         switch (reply.sfType)
  82.         {
  83.             case kJPEGType:
  84.                 theWorld = OpenJPEGFile(reply.sfFile, &err);
  85.                 break;
  86.                 
  87.             case kPICTType:    
  88.                 err = OpenPictFile(&reply.sfFile, &theWorld);
  89.                 break;
  90.                 
  91.         }
  92.         
  93.         if (err == noErr && theWorld != nil)
  94.         {
  95.             bounds = theWorld->portRect;
  96.             SetRect(&bounds, 3, GetMBarHeight() + kTitleBarHeight + kFudgeFactor,
  97.                     bounds.right + 3, bounds.bottom + GetMBarHeight()
  98.                     + kTitleBarHeight + kFudgeFactor);
  99.                     
  100.             window = CreateWindow(nil, nil, &bounds, reply.sfFile.name, false,
  101.                                   zoomDocProc, kDocKind, (WindowPtr)-1, true, nil );
  102.     
  103.             if (window != nil) 
  104.             {
  105.                 doc = (DocHnd)GetWRefCon(window);
  106.                 if (doc != nil)    
  107.                     (**doc).world = theWorld;
  108.                     (**doc).fileType = reply.sfType;
  109.             }
  110.         }                
  111.     }
  112.                               
  113.     
  114.     if (err != noErr)
  115.         HandleError(err, false);
  116.     
  117. }
  118.  
  119.     
  120.  
  121. //----------------------------------------------------------------------
  122. //
  123. //    DoSaveImage - main entry point for saving an image
  124. //                
  125. //
  126. //----------------------------------------------------------------------
  127.  
  128. OSErr DoSaveImage(WindowRef window)
  129. {
  130.     OSErr                    err = noErr;
  131.     StandardFileReply        reply;
  132.     SFTypeList                 typeList;
  133.     DlgHookYDUPP            theDialogHookUPP;
  134.     Point                    thePt = {-1, -1};
  135.     short                    numTypes;
  136.     Str255                    title;
  137.     DocHnd                    doc;
  138.  
  139.  
  140.     doc = (DocHnd)GetWRefCon(window);
  141.     if (doc != nil)
  142.     {
  143.         gFileType = (**doc).fileType;
  144.         GetWTitle(window, title);
  145.         
  146.         theDialogHookUPP = NewDlgHookYDProc(CustomPutFileHook);
  147.  
  148.         CustomPutFile((ConstStr255Param)"", title,
  149.                       &reply, 133, thePt, theDialogHookUPP,
  150.                        nil, nil, nil, nil);
  151.  
  152.         DisposeRoutineDescriptor((UniversalProcPtr)theDialogHookUPP);
  153.  
  154.         SetCursor(*GetCursor(watchCursor));
  155.         
  156.         if (reply.sfGood)
  157.         {
  158.             if (reply.sfReplacing) 
  159.                 err = FSpDelete(&reply.sfFile);
  160.             
  161.             if (err == noErr)
  162.             {
  163.                 switch (gFileType)
  164.                 {
  165.                     case kJPEGType:
  166.                         err = SaveJPEGFile(reply, window);
  167.                         if (err == noErr)
  168.                             (**doc).dirty = true;
  169.                         break;
  170.                         
  171.                     case kPICTType:    
  172.                         err = SavePictFile(&reply.sfFile, (**doc).world);
  173.                         if (err == noErr)
  174.                             (**doc).dirty = true;
  175.                         break;
  176.                         
  177.                 }
  178.             }
  179.  
  180.         }
  181.         else
  182.             if (err == userCanceledErr)
  183.                 err = noErr;
  184.         
  185.         SetCursor(&qd.arrow);
  186.     }
  187.         
  188.     return err;
  189. }
  190.  
  191.  
  192. //----------------------------------------------------------------------
  193. //
  194. //    CustomFileDlogFilter - 
  195. //                
  196. //
  197. //----------------------------------------------------------------------
  198.  
  199. pascal Boolean CustomFileDlogFilter(CInfoPBPtr theParamBlok, Ptr theDataPtr)
  200. {
  201.     Boolean        notDisplayed = true;
  202.     
  203.     switch (theParamBlok->hFileInfo.ioFlFndrInfo.fdType)
  204.     {
  205.         case kPICTType:
  206.             if (gFileType == kPICTType || gFileType == kAllType)
  207.                 notDisplayed = false;
  208.             break;
  209.         
  210.         case kJPEGType:
  211.             if (gFileType == kJPEGType || gFileType == kAllType)
  212.                 notDisplayed = false;
  213.             break;
  214.  
  215.         default:
  216.             if ((theParamBlok->dirInfo.ioFlAttrib & ioDirMask) && 
  217.                 !(theParamBlok->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible))
  218.                     notDisplayed = false;
  219.             break;
  220.     }
  221.     
  222.     
  223.     return    notDisplayed;
  224.     
  225. }
  226.  
  227.  
  228. //----------------------------------------------------------------------
  229. //
  230. //    CustomGetFileHook -
  231. //                
  232. //
  233. //----------------------------------------------------------------------
  234.  
  235. pascal short CustomGetFileHook(short item, DialogPtr theDlogPtr, void* theData)
  236. {
  237.     Handle                theHandle;
  238.     Rect                theRect;
  239.     short                theType;
  240.     short                ignored;
  241.     
  242.     switch (item)    
  243.     {
  244.         case sfHookFirstCall:
  245.             GetDItem(theDlogPtr, rCustomGetPopupID, &theType, &theHandle, &theRect);
  246.             
  247.             switch (gFileType)
  248.             {
  249.                 case kAllType:
  250.                     theType = kAllGetID;
  251.                     break;
  252.  
  253.                 case kPICTType:
  254.                     theType = kPICTGetID;
  255.                     break;
  256.                     
  257.                 case kJPEGType:
  258.                     theType = kJPEGGetID;
  259.                     break;
  260.                     
  261.             }
  262.             
  263.             SetCtlValue((ControlHandle)theHandle,theType);
  264.             item = sfHookNullEvent;
  265.             break;
  266.         
  267.         case rCustomGetPopupID:
  268.             GetDItem(theDlogPtr, item, &ignored, &theHandle, &theRect);
  269.             theType = GetCtlValue((ControlHandle)theHandle);
  270.             
  271.             switch (theType)
  272.             {
  273.                 case kAllGetID:
  274.                     gFileType = kAllType;
  275.                     item = sfHookRebuildList;
  276.                     break;
  277.                     
  278.                 case kPICTGetID:
  279.                     gFileType = kPICTType;
  280.                     item = sfHookRebuildList;
  281.                     break;
  282.                     
  283.                 case kJPEGGetID:
  284.                     gFileType = kJPEGType;
  285.                     item = sfHookRebuildList;
  286.                     break;
  287.  
  288.             }
  289.             break;
  290.     }
  291.     
  292.     return item;
  293.     
  294. }
  295.  
  296.  
  297. //----------------------------------------------------------------------
  298. //
  299. //    CustomPutFileHook - used to sort file types when saving a file
  300. //                
  301. //
  302. //----------------------------------------------------------------------
  303.  
  304. pascal short CustomPutFileHook(short item,DialogPtr theDlogPtr,void* theData)
  305. {
  306.     Handle                theHandle;
  307.     Rect                theRect;
  308.     short                theType;
  309.     short                ignored;
  310.     
  311.     if (GetWRefCon(theDlogPtr) != sfMainDialogRefCon)
  312.         return item;
  313.     
  314.     switch (item)    
  315.     {
  316.         case sfHookFirstCall:
  317.             GetDItem(theDlogPtr, rCustomPutPopupID, &theType, &theHandle, &theRect);
  318.             
  319.             switch (gFileType)
  320.             {
  321.                 case kPICTType:
  322.                     theType = kPICTPutID;
  323.                     break;
  324.                     
  325.                 case kJPEGType:
  326.                     theType = kJPEGPutID;
  327.                     break;
  328.             }
  329.             
  330.             SetCtlValue((ControlHandle)theHandle,theType);
  331.             item = sfHookNullEvent;
  332.             break;
  333.             
  334.         case rCustomPutPopupID:
  335.             GetDItem(theDlogPtr,item,&ignored,&theHandle,&theRect);
  336.             theType = GetCtlValue((ControlHandle)theHandle);
  337.             
  338.             switch (theType)
  339.             {
  340.                 case kPICTPutID:
  341.                     gFileType = kPICTType;
  342.                     break;
  343.                     
  344.                 case kJPEGPutID:
  345.                     gFileType = kJPEGType;
  346.                     break;
  347.             }
  348.             
  349.             item = sfHookNullEvent;
  350.             break;
  351.     }
  352.     
  353.     return item;
  354.     
  355. }    
  356.  
  357.  
  358. //----------------------------------------------------------------------
  359. //
  360. //    ZeroRect - zero the top-left points of a rect
  361. //                
  362. //
  363. //----------------------------------------------------------------------
  364.  
  365. void ZeroRect(Rect *r)
  366. {
  367.     Rect        zr;
  368.     
  369.     zr = *r;
  370.     
  371.     if (zr.left < 0)
  372.         OffsetRect(&zr,zr.left,0);
  373.     if (zr.top < 0)
  374.         OffsetRect(&zr,0,zr.top);
  375.     
  376.     OffsetRect(&zr,-zr.left,-zr.top);
  377.     
  378.     *r = zr;
  379.  
  380.  
  381.  
  382. //----------------------------------------------------------------------
  383. //
  384. //    pstrcpy - pascal string copy
  385. //                
  386. //
  387. //----------------------------------------------------------------------
  388.  
  389. void pstrcpy(StringPtr dst, StringPtr src)
  390. {
  391.     short    c;
  392.     
  393.     for (c = *src; c > -1; c--)
  394.         dst[c] = src[c];
  395. }
  396.  
  397.  
  398. //----------------------------------------------------------------------
  399. //
  400. //    pstrcat - pascal string concat
  401. //                
  402. //
  403. //----------------------------------------------------------------------
  404.  
  405. void pstrcat(StringPtr dst, StringPtr src)
  406. {
  407.     
  408.     short    c;
  409.  
  410.     for (c = 1; c < src[0] + 1; c++)
  411.         dst[dst[0] + c] = src[c];
  412.     dst[0] += src[0];
  413. }
  414.